<template>
	<view class="container">
		<view class="top-image">
			<image src="@/static/top.png" style="width: 750rpx" mode="widthFix"></image>
		</view>
		<view class="form-container">
			<uni-forms class="login-form" ref="accountForm" :model="formData" :rules="accountRules">
				<uni-forms-item>
					<input style="width: 650rpx" maxlength="11" type="tel" placeholder="请输入手机号" v-model="formData.mobile" class="uni-input-input" placeholder-style="color: #818181" />
				</uni-forms-item>
				<uni-forms-item>
					<view class="send-code">
						<input
							style="width: 350rpx; margin-left: 50rpx"
							maxlength="15"
							type="tel"
							v-model="formData.smsCode"
							placeholder="请输入验证码"
							class="uni-input-input"
							placeholder-style="color: #818181"
						/>
						<view class="code-line"></view>
						<view class="code" @click="sendCode">获取验证码</view>
					</view>
				</uni-forms-item>
				<view class="login-btn" style="text-align: center">
					<image src="/static/btn.png" mode="" style="width: 600rpx; height: 110rpx" @click="submitform"></image>
				</view>
				<view style="margin-top: 20rpx; text-align: center; margin-bottom: 0rpx; padding-bottom: 12rpx;display: flex;">
					<checkbox-group @change="radioChange">
						<label>
							<checkbox value="cb" :checked="allChecked" style="transform: scale(0.7)" />
							<text style="color: #f4f9ff; font-size: 26rpx">已阅读并同意</text>
						</label>
					</checkbox-group>
					<text style="color: #f4f9ff; font-weight: 600">《活动规则》《隐私条款》</text>
				</view>
			</uni-forms>
		</view>
		<view class="bottom-image">
			<image src="@/static/bottom.png" style="width: 750rpx" mode="widthFix"></image>
		</view>

		<view class="arrive-btn" :style="{ display: showHeight > 500 ? '' : 'none' }">
			<image src="/static/btn.png" mode="" style="width: 600rpx; height: 110rpx" @click="toTop"></image>
		</view>
	</view>
</template>

<script setup>
import { ref, reactive, onMounted,watchEffect } from 'vue';
import { onLoad, onPageScroll } from '@dcloudio/uni-app';
import initApi from '@/apis/init.js';
const showHeight = ref(0);
const redirectURL = ref();
const routeType = ref();
const checkValue = ref(false);
const r1 = ref();
const accountForm = ref();
const isSend = ref(false);
const isNum = ref(0);
const isTimer = ref();
// 表单数据
const formData = reactive({
	mobile: '',
	smsCode: null,
	orderNo: null,
	channelCode: '',
	productCode: ''
});
const accountRules = reactive({
	mobile: {
		rules: [
			{ required: true, errorMessage: '请输入手机号码' },
			{ pattern: /^1[3-9][0-9]\d{8}$/, errorMessage: '手机号码格式不正确' }
		]
	},
	code: {
		rules: [{ required: true, errorMessage: '请输入手机验证码' }]
	}
});
watchEffect(() => {
	const isOkMobile = /^1[3-9][0-9]\d{8}$/.test(formData.mobile);
	if (isOkMobile) {
		uni.hideKeyboard();
	}
});
// 获取传递过来的参数
onLoad((query) => {
	formData.channelCode = query.a;
	formData.productCode = query.p;
});
const radioChange = (val) => {
	if (val.detail.value.length > 0) {
		allChecked.value = true;
	} else {
		allChecked.value = false;
	}
};

const submitform = async () => {
	try {
		const isOk = await accountForm.value.validate();
		if (!formData.smsCode) return uni.utils.toast('请输入验证码');
		if (!checkValue.value) return uni.utils.toast('请勾选《活动规则》《隐私条款》');
		if (!formData.orderNo) return uni.utils.toast('订单号为空 请输入正确信息');
		const arr = {
			orderNo: formData.orderNo,
			mobile: formData.mobile,
			smsCode: formData.smsCode,
		};
		const res = await initApi.processInventoryPackageOrder(arr);
		if (res.code != 20000) return uni.utils.toast(res.message);

		clearInterval(isTimer.value);
		isNum.value = 0;
		isSend.value = false;
		// 需要一个东西来 控制多次提交
	} catch (e) {
		//TODO handle the exception
		clearInterval(isTimer.value);
		isNum.value = 0;
		isSend.value = false;
		return uni.utils.toast('数据提交失败 请重试！');
	}
};
const sendCode = async () => {
	// 如何单独校验一个数据
	if (isSend.value) return;
	try {
		const arr = {
			mobile: formData.mobile,
			channelCode: formData.channelCode,
			productCode: formData.productCode
		};
		const isOkMobile = /^1[3-9][0-9]\d{8}$/.test(formData.mobile);
		if (!isOkMobile) return uni.utils.toast('请输入正确的手机号');

		const res = await initApi.initInventoryPackageOrder(arr);
		if (res.code != 20000) return uni.utils.toast(res.message);
		formData.orderNo = res.data.orderNo;
		isNum.value = 60;
		isSend.value = true;

		isTimer.value = setInterval(() => {
			if (isNum.value <= 0) {
				clearInterval(isTimer.value);
				isNum.value = 0;
				isSend.value = false;
			}
			isNum.value--;
		}, 1000);
		return uni.utils.toast('验证码发送成功');
	} catch (e) {
		//TODO handle the exception
		isSend.value = false;
		isNum.value = 0;
		clearInterval(isTimer.value);
		uni.utils.toast('请输入正确的手机号');
	}
};
onPageScroll((e) => {
	showHeight.value = e.scrollTop;
});
const toTop = () => {
	uni.pageScrollTo({
		scrollTop: 0,
		duration: 300
	});
};
// CL0219
</script>

<style lang="scss" scoped>
.arrive-btn {
	position: fixed;
	bottom: 20rpx;
	width: 750rpx;
	text-align: center;
}
.container {
	background-color: #1da7fe;
	width: 750rpx;
	margin: 0 auto;
	overflow: hidden;
	.form-container {
		width: 750rpx;
		margin: 0 auto;
		margin-top: -50rpx;
		margin-bottom: -2rpx;
		padding: 0 25rpx;

		.login-btn {
			width: 700rpx;
			animation: scaleUp 2s infinite; /* 动画名称、持续时间、是否无限循环 */
		}
		@keyframes scaleUp {
			0%,
			50% {
				transform: scale(1); /* 开始时的缩放比例 */
			}
			25%,
			75% {
				transform: scale(1.05); /* 缩放比例最大值 */
			}
			100% {
				transform: scale(1); /* 结束时的缩放比例 */
			}
		}
		.send-code {
			display: flex;
			height: 100rpx;
			.code-line {
				height: 50rpx;
				width: 2rpx;
				border-left: 2rpx #7a7a7a solid;
				margin-top: 25rpx;
				margin-right: 6rpx;
			}
			.code {
				height: 100rpx;
				width: 200rpx;
				line-height: 100rpx;
				color: #4b5ae5;
				font-size: 32rpx;
				cursor: pointer;
				margin-left: 30rpx;
			}
		}
		.uni-forms-item {
			width: 700rpx;
			height: 100rpx;
			margin-bottom: 20 !important;
			border-bottom: 2rpx solid #eee;
			background-color: #ffffff;
			border-radius: 100rpx;
			box-sizing: border-box;
			.uni-input-input {
				width: 600rpx;
				margin-left: 50rpx;
			}
		}
		::v-deep .uni-forms-item__error {
			width: 100%;
			padding-top: 10rpx;
			color: #fff;
			text-align: center;
			font-size: 28rpx;
			transition: none;
		}
	}
}
::v-deep {
	image {
		vertical-align: bottom; // 解决image背景图片下方有10rpx 的白边
	}
}
:deep(.uni-checkbox-input) {
	border-radius: 80rpx !important;
}
</style>
